


#ifndef _BLOCK_ALLOC_H_
#define _BLOCK_ALLOC_H_


#define _SIZE_BLOCK_ 100000

#include <vector>
using namespace std;

//
//////////////////////////////////////////////////////////////////////////
template <class T> class blockalloc
{
public:
  
  blockalloc (void)
  {
    m_currRow = m_currCol = m_num = 0;
    T *newVal = new T[_SIZE_BLOCK_];
    m_values.push_back(newVal);
  }

  ~blockalloc (void)
  {
    int n = (int) m_values.size();
    for (int i = 0; i < n; ++i)
      delete []m_values[i];
    m_values.clear();
  }

  // get 
  T *get ( )
  {
    T *row = m_values[m_currRow];
    T *newItem = &(row[m_currCol]);
    m_num++;
    m_currCol++;

    if (m_currCol == _SIZE_BLOCK_)
    {
      m_currCol = 0;
      T *newVal = new T[_SIZE_BLOCK_];
      m_values.push_back(newVal);
      m_currRow++;
    }

    return newItem;
  }

  //
  int size ( )
  {
    return m_num;
  }

  //
  void clear ( )
  {
    m_currRow = m_currCol = m_num = 0;
    int n = (int) m_values.size();
    for (int i = 0; i < n; ++i)
      delete []m_values[i];
    m_values.clear();

    T *newVal = new T[_SIZE_BLOCK_];
    m_values.push_back(newVal);
  }




protected:
  long int m_num;
  vector <T *> m_values;

  int m_currRow, m_currCol;

};


#endif